﻿using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers;
using System;
using System.Collections.Generic;
using System.Data;
using static Microsoft.ML.DataOperationsCatalog;

namespace Criss.Sentiment
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("情感分析二次元");

            MLContext mlContext = new MLContext();

            var list = new List<SentimentData>();

            list.Add(new SentimentData { SentimentText = "我今天很开心，我赚了很多钱", Label = true });
            list.Add(new SentimentData { SentimentText = "哈哈，我今天赚了100块钱", Label = true });
            list.Add(new SentimentData { SentimentText = "哈哈，我今天也赚了100块钱", Label = true });
            list.Add(new SentimentData { SentimentText = "哈哈，貌似你今天也赚了100块钱", Label = true });
            list.Add(new SentimentData { SentimentText = "哈哈，我们今天收入都不错都赚了这么多钱", Label = true });
            list.Add(new SentimentData { SentimentText = "哈哈，看你们动作猛如虎，一看工资250", Label = false });
            list.Add(new SentimentData { SentimentText = "哎，今天运气不好，输了很多钱", Label = false });
            list.Add(new SentimentData { SentimentText = "哎，今天运气不好，输了600块钱", Label = false });
            list.Add(new SentimentData { SentimentText = "哎，今天非常运气不好，输了2600块钱", Label = false });
            list.Add(new SentimentData { SentimentText = "哎，今天特别运气不好，输了600块钱", Label = false });
            list.Add(new SentimentData { SentimentText = "哎，我今天才赚了200块钱", Label = false });
            list.Add(new SentimentData { SentimentText = "呵呵，我也输了钱了", Label = false });
            list.Add(new SentimentData { SentimentText = "活该你们输钱，我今天赚了100块钱", Label = false });

            var dataView = mlContext.Data.LoadFromEnumerable(list); //创建培训数据

            //var da = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.1); //不求理解，应该是分割数据的

            //var row = mlContext.Data.CreateEnumerable<SentimentData>(da.TestSet, true);//查看数据中的数据
            //var rowm = mlContext.Data.CreateEnumerable<SentimentData>(da.TestSet, true, true); //查看数据中的数据
            //var rowd = mlContext.Data.CreateEnumerable<SentimentData>(da.TrainSet, true);//查看数据中的数据
            //var rowmd = mlContext.Data.CreateEnumerable<SentimentData>(da.TrainSet, true, true);//查看数据中的数据

            var dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentData.SentimentText));//数据映射规则应该是

            //设置训练算法  然后配置 model           
            var trainer = mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features"); //指定分析算法,字段和结果字段

            var trainingPipeline = dataProcessPipeline.Append(trainer);

            // 训练模型
            ITransformer trainedModel = trainingPipeline.Fit(dataView);

            //评估模型
            //var predictions = trainedModel.Transform(dataView);
            //var metrics = mlContext.BinaryClassification.Evaluate(data: predictions, labelColumnName: "Label", scoreColumnName: "Score");

            var name = new System.IO.MemoryStream();

            mlContext.Model.Save(trainedModel, dataView.Schema,name); //这是保存到内存流

            var newTrainedModel = mlContext.Model.Load(name, out DataViewSchema ddd);//这里读取出来

            //使用模型
            var predEngine = mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(newTrainedModel); //创建模型
           
            var resultprediction = predEngine.Predict(new SentimentData { SentimentText = "哎，我今天赚了600块钱" });
            Console.WriteLine(resultprediction.Score + "------------------" + resultprediction.Probability + "-----------------------" + resultprediction.Prediction);


            resultprediction = predEngine.Predict(new SentimentData { SentimentText = "哎，活该我今天倒霉，输钱了" });
            Console.WriteLine(resultprediction.Score + "------------------" + resultprediction.Probability + "-----------------------" + resultprediction.Prediction);

            resultprediction = predEngine.Predict(new SentimentData { SentimentText = "哈哈，我今天输了600块钱，比你少" });
            Console.WriteLine(resultprediction.Score + "------------------" + resultprediction.Probability + "-----------------------" + resultprediction.Prediction);

            Console.ReadKey();
        }
    }
}
